// // 文章


//  调用方法

// $.post('http://http://api.juzhen05.com/admin/matrixbox/list?id=23')  单一数据
// $.post('http://http://api.juzhen05.com/admin/matrixbox/list')        所有数据


var path = require('path')
var routerApi = require('../../../utils/routeString.js')(__filename, __dirname);
// var SqlHelper = require('../../../utils/sqlHelper.js')

var MartixBox = require('../dal/matrixbox.js')

var plusRd = function (mb, result) {
    if (result.length > 0) {
        mb.sqlHelper.query('update matrixbox set clicks=clicks+"1" where id=' + mb.primeryKey.id, null, null);
    }
}

var list = async function (req, res, next) {
    var body = req.body || req.query;

    if (body.id) {
        var id = parseInt(body.id);

        // 命令解析
        if (isNaN(id)) {
            return res.send({ code: 0, msg: '无效请求, id参数出错' });
        }

        try {
            var mb = new MartixBox({ id: id });
            var result = await mb.select();
            // 热度+1
            plusRd(mb, result);
            return res.send({ code: 1, result: { list: result, comments: comments } });
        } catch (error) {
            return res.send({ code: 0, msg: error });
        }
    } else {
        var mb = new MartixBox({});

        try {
            var result = await mb.selectAll(null, body.offset, body.limit);
            var result2 = await mb.count() //results[0].num
            var comments = await mb.comments();
            var rdRank = await mb.selectAll(null, 0, 10, 'clicks', "DESC");
            var timeRank = await mb.selectAll(null, 0, 10, 'uploadtime', "DESC");
            return res.send({
                code: 1,
                result: {
                    list: result,
                    count: result2[0].num,
                    comments: comments,
                    rdRank: rdRank,
                    timeRank: timeRank
                }
            });
        } catch (error) {
            return res.send({ code: 0, msg: error.message });
        }
    }

}


server.get({ path: routerApi, name: routerApi }, list)
